//
//  TbRecordList.swift
//  ANOA
//
//  Created by 邱仙凯 on 2024/8/29.
//  Copyright © 2024 zwb. All rights reserved.
//

import UIKit
 

class TbRecordList:UITableView,UITableViewDelegate,UITableViewDataSource {
    var dataArray = [TBRecordResult]()
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        self.delegate = self
        self.dataSource = self
        if #available(iOS 15.0, *) {
            self.sectionHeaderTopPadding = 0
                }
      
        self.rowHeight = 35
        self.showsVerticalScrollIndicator = false
        self.register(TbRecordHeaderView.self, forHeaderFooterViewReuseIdentifier: TbRecordHeaderView.identifier)
        self.register(TbRecordListCell.self, forCellReuseIdentifier: TbRecordListCell.identifier)
        self.separatorStyle = .none
        self.tableHeaderView = tabHeaderV
    }
    var tabHeaderV : UIView = {
        let v = UIView()
        v.frame = CGRect(x: 0, y: 0, width: KSGlobal.screenW, height: 35)
        let title = UILabel()
         
        title.text = "出价记录"
        title.textAlignment = .center
        title.font = UIFont.boldSystemFont(ofSize: 17)
        v.addSubview(title)
        title.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        return v
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35
        
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: TbRecordHeaderView.identifier) as! TbRecordHeaderView
        
 
        return headerView
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: TbRecordListCell.identifier, for: indexPath) as? TbRecordListCell else {
            fatalError()
        }
        let one = dataArray[indexPath.row]
        cell.lab1.text = "\(indexPath.row + 1)"
        if one.z_bid_state == "1" {
            cell.lab2.text = one.z_price.unwrap + "元(成交)"
            cell.lab2.textColor = .red

        } else {
            cell.lab2.text = one.z_price.unwrap + "元"
            cell.lab2.textColor = .lightGray
        }
        cell.lab3.text = one.z_time_stamp
       
        return cell
    }
  
    
}


class TbRecordHeaderView: UITableViewHeaderFooterView {
    
  
    var title: UILabel!
    var title2: UILabel!
    var title3: UILabel!
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
       
        title = UILabel()
        title.text = "序号"
        title.font = UIFont.boldSystemFont(ofSize: 14)
        title.tintColor = .darkGray
        title.numberOfLines = 2
        contentView.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(10)
            make.centerY.equalTo(self.contentView)
        }
        title2 = UILabel()
        title2.text = "出价"
        title2.font = UIFont.boldSystemFont(ofSize: 14)
        title2.tintColor = .darkGray
        title2.numberOfLines = 2
        contentView.addSubview(title2)
        title2.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(53)
            make.centerY.equalTo(self.contentView)
        }
        title3 = UILabel()
        title3.text = "时间"
        title3.font = UIFont.boldSystemFont(ofSize: 14)
        title3.tintColor = .darkGray
        title3.numberOfLines = 2
        contentView.addSubview(title3)
        title3.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView).offset(135)
            make.centerY.equalTo(self.contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
}
class TbRecordListCell: UITableViewCell {
    
    var btnClosure:((String)->())?
    var lab1:UILabel!
    var lab2:UILabel!
    var lab3:UILabel!
  
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        
        self.backgroundColor = .white
        lab1 = UILabel()
        lab1.textColor = .lightGray
        lab1.font = UIFont.systemFont(ofSize: 14)
        lab2 = UILabel()
        lab2.textColor = .lightGray
        lab2.font = UIFont.systemFont(ofSize: 14)
        lab2.numberOfLines = 2
        lab3 = UILabel()
        lab3.textColor = .lightGray
        lab3.font = UIFont.systemFont(ofSize: 14)
        
        self.addSubview(lab1)
        self.addSubview(lab2)
        self.addSubview(lab3)
        
        
        lab1.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(self).offset(10)
        }
        lab2.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(53)
            make.centerY.equalTo(self)

        }
        lab3.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)

            make.left.equalTo(self).offset(135)
        }
       
    }
    
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
import ObjectMapper
struct TBRecordResult: Mappable {
    var z_bid_state:String?
    var z_price: String?
    var z_time_stamp: String?
    var z_unit_nm:String?
    init?(map: Map) {
        
    }
    // Mappable
    mutating func mapping(map: Map) {
        z_bid_state <- map["z_bid_state"]

        z_price <- (map["z_price"],StringTransform_test())
        z_time_stamp <- map["z_time_stamp"]
        z_unit_nm <- map["z_unit_nm"]

    }
    
}


 
